package com.ruoyi.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

import cn.hutool.core.map.MapUtil;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.WorkTypeMapper;
import com.ruoyi.system.domain.WorkType;
import com.ruoyi.system.service.IWorkTypeService;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-29
 */
@Service
public class WorkTypeServiceImpl implements IWorkTypeService {
    @Autowired
    private WorkTypeMapper workTypeMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public WorkType selectWorkTypeById(Long id) {
        return workTypeMapper.selectWorkTypeById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param workType 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<WorkType> selectWorkTypeList(WorkType workType) {
        return workTypeMapper.selectWorkTypeList(workType);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param workType 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertWorkType(WorkType workType) {
        workType.setCreateTime(DateUtils.getNowDate());
        int i = workTypeMapper.insertWorkType(workType);
        syncRedisState();
        return i;
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param workType 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateWorkType(WorkType workType) {
        workType.setUpdateTime(DateUtils.getNowDate());
        int i = workTypeMapper.updateWorkType(workType);
        syncRedisState();
        return i;

    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteWorkTypeByIds(Long[] ids) {
        int i = workTypeMapper.deleteWorkTypeByIds(ids);
        syncRedisState();
        return i;

    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteWorkTypeById(Long id) {
        int i = workTypeMapper.deleteWorkTypeById(id);
        syncRedisState();
        return i;
    }

    @Override
    public List<WorkType> getWorkTypeList(WorkType workType) {
        return workTypeMapper.selectWorkTypeList(workType);
    }

    @Autowired
    private SysDictDataServiceImpl sysDictDataService;

    @Autowired
    private SysDictTypeServiceImpl sysDictTypeService;

    static Map<String, String> WORKTYPE_DICT_RELEATION = new HashMap<>();

    static {
        WORKTYPE_DICT_RELEATION.put("2", "hgzdhj");
        WORKTYPE_DICT_RELEATION.put("3", "bjzdhj");
        WORKTYPE_DICT_RELEATION.put("4", "xsgzdhj");
        WORKTYPE_DICT_RELEATION.put("5", "qjgzdhj");

    }


    @Override
    public Map<String, List<SysDictData>> getWorkTypeDict(String workTypeId) {
        if (Objects.isNull(WORKTYPE_DICT_RELEATION.get(workTypeId))) {
            return MapUtil.empty();
        }

        String dictType = WORKTYPE_DICT_RELEATION.get(workTypeId);

        SysDictData sysDictData = new SysDictData();

        sysDictData.setDictType(dictType);

        List<SysDictData> selectDictDataList = sysDictDataService.selectDictDataList(sysDictData);

        List<String> dictValues = selectDictDataList.stream().map(x -> x.getDictValue()).collect(Collectors.toList());

        List<SysDictData> sysDictDataList = sysDictDataService.selectDictDataByTypeLists(dictValues);

        Map<String, List<SysDictData>> collect = sysDictDataList.stream().collect(Collectors.groupingBy(SysDictData::getDictType));

        return collect;

    }


    @Autowired
    private RedisTemplate redisTemplate;

    private void syncRedisState() {
    }
}
